$(document).ready(function() {
	//回车模糊查询
	searchEvent();
	//初始化数据
	init(1);
	
	
	/*$("#area .opts .selected").bind('DOMNodeInserted', function(e) { 
		var areaCode = $('#area .opts .selected').attr("val");
    	var areaName = $('#area .opts .selected').html();
		console.log(areaCode);console.log(areaName);
	}); */
	
	
});
	
//默认加载  
function init(pageNo){
	//获取信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/feedback/jpa/page?pageNum="+pageNo+"&pageSize="+pageSize+"&feedbackType="+$("#change").val(),
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){	
					var id = n.id;
					var feedbackType=feedbackTypeStr(n.feedbackType);
					var thislist = 
					'	<tr>'+				
					'		<td>'+n.id+'</td>'+
					'		<td>'+n.feedbackDetail+'</td>'+
					'		<td>'+feedbackType+'</td>'+						
					'		<td>'+n.gmtDatetime+'</td>'+	
					'		<td>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="deleteeach('+id+')"> 删除</a>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#details" onclick="thisDetails('+id+')"> 详情</a>'+
					'		</td>'+
					'	</tr>';
					$('#thislist').append(thislist);	
														
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});
}
//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber); 
}

/**
 * 模糊查询
 */
function searchEvent(){
	$("#change").change(function () {
		init(1);
	});
}


//删除
function deleteeach(id){
	if(confirm("确定要删除吗？")) {
		deletegq(id);
	}
}

function deletegq(id){
	$.ajax({
		url: urlcore + "/api/feedback/deleteFeedBack?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				init(1);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//投诉详情
function thisDetails(id) {
	$.ajax({
		url: urlcore + "/api/feedback/feedBackDetails?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				var da = data.data;
				var imgUrl = da.imgUrl;
				var imgStr = '';
				if (imgUrl != null && imgUrl != '') {
	                  var imgarr = imgUrl.split("***");
	                  $.each(imgarr,function(index,value){
	                  	imgStr += "<img style='padding: 5px 5px 5px 5px;' width='250px' height='250px' src='"+value+"'>";
	                  });
	            }
	            $('#imgUrl').html(imgStr);
				$('#feedbackDetail').text(da.feedbackDetail);
				$('#gmtDatetime').text(da.gmtDatetime);
				$('#User').text(da.user.realName);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

/**
 * 用户状态返回
 * @param {Object} feedbackType
 */
function feedbackTypeStr(feedbackType){
	if (feedbackType==1) {
		feedbackType = '投诉';
	}else if(feedbackType==2) {
		feedbackType = '建议';
	}
	else if(feedbackType==3) {
		feedbackType = '其他';
	}
	return feedbackType;
}
